package com.tchepannou.limbe.mail;

import com.tchepannou.util.MimeUtil;

import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import java.io.IOException;
import java.io.InputStream;

/**
 * User: herve
 * Date: 13-10-21 2:35 PM
 */
public class InputStreamAttachment implements Attachment
{
    private String _filename;
    private InputStream _stream;

    public InputStreamAttachment(String filename, InputStream stream)
    {
        _filename = filename;
        _stream = stream;
    }

    @Override
    public MimeBodyPart toBodyPart() throws MessagingException, IOException
    {
        String contentType = MimeUtil.getInstance().getMimeTypeByFile(_filename);

        MimeBodyPart attachment = new MimeBodyPart();
        attachment.setFileName(_filename);
        attachment.setContent(_stream, contentType);

        return attachment;
    }
}
